<?php

namespace App\Http\Controllers\Api;

use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\Http\Services\Wechat\WechatLogin;
use App\Images;
use App\UserInfo;
use App\User;

use Blocktrail\CryptoJSAES\CryptoJSAES;
use Endroid\QrCode\QrCode;
use Faker\Provider\de_CH\Company;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UCenterController extends Controller
{
    /**
     * 获取用户信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserInfo(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        /** @var UserInfo $thisUserInfo */
        $thisUserInfo = UserInfo::whereUserId($user->id)->first();
        if($thisUserInfo){
            $thisUserInfo->photo_url = FileManager::getImgUrl($thisUserInfo->photo_id);

            if($thisUserInfo->sex == 0){
                $thisUserInfo->sex = '保密';
            }elseif($thisUserInfo->sex == 1){
                $thisUserInfo->sex = '男';
            }else{
                $thisUserInfo->sex = '女';
            }

            if(is_null($thisUserInfo->province) || $thisUserInfo->province == ''){
                $thisUserInfo->province = '';
            }

            if(is_null($thisUserInfo->city) || $thisUserInfo->city == ''){
                $thisUserInfo->city = '';
            }

            if(is_null($thisUserInfo->area) || $thisUserInfo->area == ''){
                $thisUserInfo->area = '';
            }

            if(is_null($thisUserInfo->address) || $thisUserInfo->address == ''){
                $thisUserInfo->address = '';
            }

            if(is_null($thisUserInfo->home_province) || $thisUserInfo->home_province == ''){
                $thisUserInfo->home_province = '';
            }

            if(is_null($thisUserInfo->home_city) || $thisUserInfo->home_city == ''){
                $thisUserInfo->home_city = '';
            }

            if(is_null($thisUserInfo->home_area) || $thisUserInfo->home_area == ''){
                $thisUserInfo->home_area = '';
            }

            if(is_null($thisUserInfo->home_address) || $thisUserInfo->home_address == ''){
                $thisUserInfo->home_address = '';
            }


        $user->user_info        = $thisUserInfo;
        return response()->json(['level' => 'success','message' => '获取成功','data' => $user]);
    }
    }
}
